package com.ysyc.driver_ee.widget;

import android.content.Context;
import android.support.annotation.StringRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;


import com.ysyc.driver_ee.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoadingDialog extends DialogHelp {


    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.loading_title)
    TextView loadingTv;
    private RotateAnimation rotateAnimation;

    public LoadingDialog(Context context) {
        super(context);
        initView();
    }

    private void initView() {
        View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_loading, null);
        ButterKnife.bind(this, dialogView);
        setContentView(dialogView);
        //loading image的动画
        loadingAnimation();


    }

    public void loadingAnimation() {
        if (rotateAnimation==null) {
            rotateAnimation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            rotateAnimation.setDuration(1500);
            rotateAnimation.setRepeatCount(-1);
            rotateAnimation.setInterpolator(new LinearInterpolator());
            rotateAnimation.setFillAfter(true);
        }
        img.startAnimation(rotateAnimation);
        setOnDismissListener(dialog -> rotateAnimation.cancel());
    }


    public LoadingDialog setLoadingTxt(@StringRes int string) {
        loadingTv.setText(getContext().getResources().getString(string));
        return this;
    }

}
